package org.dochandler.esfinge.guardian.authorizer;

import org.dochandler.service.domain.Officer;
import org.dochandler.service.domain.annotation.CommandersOnly;
import org.dochandler.service.domain.constants.Constants;
import org.dochandler.service.exception.OfficerNotDefinedException;
import org.esfinge.guardian.authorizer.Authorizer;
import org.esfinge.guardian.context.AuthorizationContext;

public class CommandersOnlyAuthorizer implements Authorizer<CommandersOnly> {

	@Override
	public Boolean authorize(AuthorizationContext context, CommandersOnly securityAnnotation) {
		
		Officer officer = context.getSubject().get( Constants.OFFICER.val(), Officer.class );
		if (officer == null) 
			throw new OfficerNotDefinedException("An officer must be defined at the appropriate scope");
		
		if (officer.isCommander() == true) {
			return true;
		}
		
		return false;
	}

}